package org.example.medicalstaff.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.example.his.common.api.CommonResponse;
;
import org.example.medicalstaff.entity.CheckRelation;
import org.example.medicalstaff.entity.CheckTemplate;
import org.example.medicalstaff.entity.FmedItem;
import org.example.medicalstaff.mapper.CheckRelationMapper;
import org.example.medicalstaff.mapper.CheckTemplateMapper;
import org.example.medicalstaff.mapper.FmedItemMapper;
import org.example.medicalstaff.service.CheckService;
import org.example.medicalstaff.utils.DateStringFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 周琛钧v0.1.1:URL基本都是经过修改的，只能根据数据表以及请求的格式来判断方法的写法
 */
@Service
public class CheckServiceImpl implements CheckService {
    @Autowired
    CheckTemplateMapper checkTemplateMapper;
    @Autowired
    CheckRelationMapper checkRelationMapper;
    @Autowired
    CheckTemplate checkTemplate;
    @Autowired
    CheckRelation checkRelation;
    @Autowired
    FmedItemMapper fmedItemMapper;

    @Override
    public CommonResponse<CheckTemplate> check_manageTemplate_add
        (String Name,
        int Scope,
        int RecordType,
        int UsersID,
        String CreationTime

    ){      CheckTemplate checkTemplate1=new CheckTemplate();
            checkTemplate1.setUserID(UsersID);
            checkTemplate1.setName(Name);
            String pattern = "yyyy-MM-dd HH:mm:ss";
            Date date = DateStringFormat.parseDateTimeString(CreationTime,pattern);
            checkTemplate1.setCreationTime(date);
            checkTemplate1.setScope(Scope);
            checkTemplate1.setRecordType(RecordType);

            int a =checkTemplateMapper.insert(checkTemplate1);

            if (a==1)
                return CommonResponse.createForSuccess("新增模板成功",checkTemplate1);
            else
                return CommonResponse.createForError(1,"新增模板失败");
    }

    @Override
    public CommonResponse<CheckTemplate> check_manageTemplate_delete(
            int TempID
    ) {
        QueryWrapper<CheckTemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ID",TempID);
        CheckTemplate checkTemplate1=new CheckTemplate();
        checkTemplate1=checkTemplateMapper.selectOne(queryWrapper);
        int ID=checkTemplate1.getID();
        System.out.println(ID);

        int result2=checkRelationMapper.deleteById(ID);
        int result1 =checkTemplateMapper.delete(queryWrapper);
        if(result1==1&&result2==1){
            return CommonResponse.createForSuccessMessage("删除模板成以及明细成功");
        }
        else return CommonResponse.createForError(1,"删除失败");
    }

    @Override
    //
    public CommonResponse<CheckTemplate> check_manageTemplate_update(
            String name,
            int scope,
            int recordType,
            int usersID,
            String creationTime
            ) { CheckTemplate checkTemplate1=new CheckTemplate();
                checkTemplate1.setName(name);
                checkTemplate1.setUserID(usersID);
                checkTemplate1.setScope(scope);
                checkTemplate1.setRecordType(recordType);
                String pattern = "yyyy-MM-dd HH:mm:ss";
                Date date = DateStringFormat.parseDateTimeString(creationTime,pattern);
                checkTemplate1.setCreationTime(date);
                QueryWrapper<CheckTemplate> queryWrapper=new QueryWrapper<>();
                queryWrapper.like("Name",name);
                queryWrapper.eq("UserID",usersID);
                queryWrapper.eq("CreationTime",creationTime);
                //忘记ID是主键，只能用原来的不然修改有问题，这是后面加的，理论上应该没问题
                CheckTemplate checkTemplate2=new CheckTemplate();
                checkTemplate2=checkTemplateMapper.selectOne(queryWrapper);
                checkTemplate1.setID(checkTemplate2.getID());

                int result=checkTemplateMapper.update(checkTemplate1,queryWrapper);
                if (result==1)
                    return CommonResponse.createForSuccessMessage("修改成功");
                else
                    return CommonResponse.createForError(1,"修改失败");

    }

    @Override
    public CommonResponse<List<CheckTemplate>> check_manageTemplate_getList() {
        List<CheckTemplate>list=checkTemplateMapper.selectList(null);
        if(list.size()>0)
            return CommonResponse.createForSuccess("获取模板成功",list);
        else
            return CommonResponse.createForError(1,"获取模板失败");
    }

    @Override
    public CommonResponse<CheckTemplate> check_manageTemplate_get(String name) {
        QueryWrapper<CheckTemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("Name",name);
        checkTemplate=checkTemplateMapper.selectOne(queryWrapper);
        if(checkTemplate!=null)
            return CommonResponse.createForSuccess("获取模板成功",checkTemplate);
        else
            return CommonResponse.createForError(1,"获取模板失败");

    }

    @Override
    public CommonResponse<CheckRelation> check_manageRelation_add(
            int CheckProjID,
            int CheckTempID ,
            String Position) {
                CheckRelation checkRelation1=new CheckRelation();
                checkRelation1.setCheckTempID(CheckTempID);
                checkRelation1.setPosition(Position);
                checkRelation1.setCheckProjID(CheckProjID);
                int result=checkRelationMapper.insert(checkRelation1);
                if (result==1)
                    return CommonResponse.createForSuccessMessage("新增成功");
                else
                    return CommonResponse.createForError(1,"新增失败");
    }

    @Override
    public CommonResponse<CheckRelation> check_manageRelation_delete(
            int CheckProjID,
            int CheckTempID
    ) {
        QueryWrapper<CheckRelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("CheckProjID",CheckProjID);
        queryWrapper.eq("CheckTempID",CheckTempID);
        checkRelation=checkRelationMapper.selectOne(queryWrapper);
        int ID= checkRelation.getID();
        int result=checkRelationMapper.deleteById(ID);
        if(result==1){
            return CommonResponse.createForSuccessMessage("删除成功");
        }
        else return CommonResponse.createForError(1,"删除失败");
    }

    @Override
    public CommonResponse<CheckTemplate> check_manageRelation_getList(String checkTempName) {
        QueryWrapper<CheckTemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("Name",checkTempName);
        CheckTemplate checkTemplate1 = checkTemplateMapper.selectOne(queryWrapper);
    if(checkTemplate1 != null)
        return CommonResponse.createForSuccess("获取模板成功",checkTemplate1);
    else
        return CommonResponse.createForError(1,"获取模板失败");
    }

    @Override
    public CommonResponse<List<FmedItem>> check_manageFmedItem_getList(int checkTempID) {
        QueryWrapper<CheckRelation> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("CheckTempID",checkTempID);
        List<CheckRelation> list=checkRelationMapper.selectList(queryWrapper);
        List<FmedItem> fmedItemList = new ArrayList<>();
        for(int i=0;i<list.size();i++) {
            int a = list.get(i).getCheckProjID();
            FmedItem fmedItem = fmedItemMapper.selectById(a);
            fmedItemList.add(fmedItem);
        }
        if(fmedItemList.size()>0)
            return CommonResponse.createForSuccess("获取模板明细成功",fmedItemList);
        else
            return CommonResponse.createForError(1,"获取模板明细失败");
    }


}
